package com.example.hello;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class food_data implements Runnable{

        private Handler handler;
        private static final String TAG ="ratelist2" ;
        public void setHandler(Handler handler) {
            this.handler = handler;}
    @Override
    public void run() {

        Document doc = null;
      ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String, String>>();
        try {
            doc = Jsoup.connect("https://www.pcbaby.com.cn/tools/heat/").get();
            Log.i(TAG,"run:title=" + doc.title());

            Elements tables = doc.getElementsByTag("table");//获取table对象

            for (int i = 0; i < tables.size() - 1;i++) {
                Element table = tables.get(i);
                Elements tds = table.getElementsByTag("td");

                for (int x = 0; x < tds.size() - 4; x += 2) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("ItemTitle", tds.get(x).text());
                    map.put("ItemDetail", tds.get(x + 1).text() + "g");
                    listItems.add(map);
                }
                Log.i(TAG,"run:title=" + listItems);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(9,listItems);
        handler.sendMessage(msg);

    }
    }

